package edu.nf.meituan.service.shop.impl;

import edu.nf.meituan.dao.shop.ShopDAO;
import edu.nf.meituan.dao.shop.impl.ShopDAOImpl;
import edu.nf.meituan.model.shop.Shop;
import edu.nf.meituan.service.exception.CodeException;
import edu.nf.meituan.service.shop.ShopService;
import edu.nf.util.sms.SMSUtil;

import java.util.List;

/**
 * @Date 2023-06-20
 * @Author qiu
 * 商家业务实现类
 */
public class ShopServiceDaoImpl implements ShopService {
    private ShopDAO shopDAO = new ShopDAOImpl();

    @Override
    public int registerShop(int typeId, int uId, String shopName, String shopPhone, String shopAddress, String key) {
        int row = 0;

        Shop shop = new ShopDAOImpl().equeryPhone(shopPhone);
        if (shop != null) {
            throw new RuntimeException("该手机已注册！");
        }

        // 判断输入的验证码和生成的是否一致
        String send = new SMSUtil().send(shopPhone);
        if (key.trim().equals(send)) {
            row = new ShopDAOImpl().registerShop(typeId, uId, shopName, shopPhone, shopAddress);
            return row;
        }
        try {
            throw new CodeException("验证码错误");
        } catch (CodeException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Shop> listShopByShopName(String shopName) {
        return shopDAO.listShopByShopName(shopName);
    }


}
